package com.chinasoft.mall.activity;

import java.io.Serializable;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.framework.LivingGoods;
import com.chinasoft.mall.framework.OrderGoods;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.EpgBean;
import com.chinasoft.mall.model.response.GetGameBean;
import com.chinasoft.mall.model.response.GetGameBean.Data;
import com.chinasoft.mall.receiver.myBroadCast;

@SuppressLint("HandlerLeak")
public abstract class BaseObserverActivity extends Activity implements
		ControllerCenter.Successcallback, ControllerCenter.Failcallback,
		Observer {
	// 控制器
	protected ControllerCenter control;
	// 数据bean
	protected Serializable sz;

	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			obserData oD = (obserData) msg.obj;
			updateActivity(oD.observable, oD.data);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sz = getIntent().getSerializableExtra(Config.INTENT_OBJ);
	}

	@Override
	protected void onResume() {
		super.onResume();
		control = ControllerCenter.getInstance();
		control.setFailcallback(this);
		control.setSuccesscallback(this);
		control.setMyContext(this);

		LivingGoods.getInstance().addObserver(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void fail(BaseBean obj) {

	}

	@Override
	public void success(BaseBean obj) {
		if (Config.GET_GAME.equals(obj.getReq_url())) {
			if (obj.getResData() != null&&obj.getResData().getData() != null) {
				GetGameBean _ggb = (GetGameBean) obj.getResData().getData();
				EpgBean _eb = new EpgBean();
				if (_ggb.getContent() != null && !"".equals(_ggb.getContent())
						&& !OrderGoods.prompt_bg.contains(_ggb.getContent())) {
					_eb.setTHUM(_ggb.getContent());
					OrderGoods.prompt_bg.add(_ggb.getContent());
					Intent intent = new Intent(myBroadCast.ACTION_INTENT_TEST);
					intent.putExtra(Config.INTENT_OBJ, _eb);
					((MainActivity) this.getParent()).sendBroadcast(intent);
				}
				List<Data> lds = _ggb.getDatas();
				if (lds != null && lds.size() > 0) {
					for(Data d:lds){
						if("yyl".equals(d.getType())&&!OrderGoods.goodsids.contains(d)){
							((MainActivity) this.getParent()).showGame(d);
							break;
						}
					}
				}
			}
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		obserData oD = new obserData();
		oD.observable = observable;
		oD.data = data;
		Message msg = new Message();
		msg.obj = oD;
		myHandler.sendMessage(msg);
	}

	public abstract void updateActivity(Observable observable, Object data);

	class obserData {
		Observable observable;
		Object data;
	}
}
